import { AccountService } from './../../service/accountService';
import { ServiceManager } from './../../common/serviceFactory/serviceManager';
import * as Koa from 'koa';
import { route, RouteOptionsBase } from "../../common";
import { AuthenService } from '../../service/authenService';
import { ServiceException } from '../../common/excpetion';
import { UserService } from '../../service/userService';
import * as captchapng from 'captchapng2';
import { VerifyCodeManager } from '../../service/verifyCodeManager';
const authSvr = ServiceManager.Current.GetService<AuthenService>('AuthenService');
const accountSvr = ServiceManager.Current.GetService<AccountService>('AccountService');
const userSvr = ServiceManager.Current.GetService<UserService>('UserService');

class AuthController {
    @route(new RouteOptionsBase('put', '/api/signIn'))
    async signIn(ctx: Koa.Context, next) {
        let reqData: SignInReq = ctx.request.body;

        if (!reqData.orgId) {
            //表示是配置后台登录
            let account = await accountSvr.GetAccountByUsername(reqData.username);
            if (!account) {
                throw new ServiceException('账号不存在！');
            }
            let user = await userSvr.GetUserByAccountId(account.id);
            let token = await authSvr.SaveLoginToken(account, ctx.ip);
            ctx.response.body = { account: account, token, user };
        }
        else {
            let user = await userSvr.GetUserByPhone(reqData.username, reqData.orgId);
            if (!user) {
                throw new ServiceException('当前单位不存在该人员！');
            }

            let account = await accountSvr.GetAccountById(user.accountId);
            if (!account || !user) {
                throw new ServiceException('账号不存在！');
            }

            if (account.username != reqData.username) {
                throw new ServiceException('账号不存在！');
            }

            if (!reqData.verifyCode.code) {
                throw new ServiceException('未填写校验码！');
            }

            if (account.password != reqData.password) {
                throw new ServiceException('密码错误!');
            }

            VerifyCodeManager.Current.Verify(reqData.verifyCode.id, reqData.verifyCode.code);

            let token = await authSvr.SaveLoginToken(account, ctx.ip);

            ctx.response.body = { account: account, token, user };
        }
        await next();
    }

    @route(new RouteOptionsBase('get', '/api/verifyCode'))
    async getVerifyCodeImg(ctx: Koa.Context, next) {
        let verfiyCodeObj = VerifyCodeManager.Current.GetVerifyCode();
        let png = new captchapng(80, 30, verfiyCodeObj.code); // width,height, 
        let buf: Buffer = png.getBuffer();

        ctx.response.body = {
            id: verfiyCodeObj.id,
            code: 'data:image/png;base64,' + buf.toString('base64')
        }
        await next();
    }
}

interface SignInReq {
    username: string;
    password: string;
    orgId?: string;
    verifyCode?: {
        id: string;
        code: number;
    };
}